package org.jeecg.modules.hc.controller;

import com.alipay.api.AlipayApiException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.hc.entity.HcAliPay;
import org.jeecg.modules.hc.service.IHcAliPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @Description: 支付宝支付
 * @Author: jeecg-boot
 * @Date: 2023-01-12
 * @Version: V1.0
 */
@Api(tags = "支付宝支付")
@RestController
@RequestMapping("/hc/hcAliPay")
@Slf4j
public class HcAliPayController extends JeecgController<HcAliPay, IHcAliPayService> {
    @Autowired
    private IHcAliPayService hcAliPayService;

    @ApiOperation("/回调")
    @RequestMapping("/notify")
    public String notify(HttpServletRequest request) {
        Map<String, String> params = new HashMap<>();
        Map requestParams = request.getParameterMap();
        for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }
            //乱码解决，这段代码在出现乱码时使用。
            //valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put(name, valueStr);
        }

        log.info("支付宝支付回调参数:" + params);
        boolean signVerified = false;

        try {
            signVerified = hcAliPayService.paynotify(params);
            log.info("支付宝支付验签结果：" + signVerified);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("支付宝支付结果通知接口服务端异常,异常信息---" + e.getMessage(), e);
            return "fail";
        }

        if (signVerified) {
            return "success";
        } else {
            return "fail";
        }
    }

    /**
     * 预支付
     *
     */

    @ApiOperation(value = "支付宝支付-预支付", notes = "支付宝支付-预支付")
    @PostMapping(value = "/prePay")
    public Result<String> prePay() throws AlipayApiException {
        hcAliPayService.preNativePay("1638010999770451970",new BigDecimal(0.01));
        return Result.OK("添加成功！");
    }


    /**
     * 分页列表查询
     *
     * @param hcAliPay
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "支付宝支付-分页列表查询")
//    @ApiOperation(value = "支付宝支付-分页列表查询", notes = "支付宝支付-分页列表查询")
//    @GetMapping(value = "/list")
    public Result<IPage<HcAliPay>> queryPageList(HcAliPay hcAliPay,
                                                 @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                 HttpServletRequest req) {
        QueryWrapper<HcAliPay> queryWrapper = QueryGenerator.initQueryWrapper(hcAliPay, req.getParameterMap());
        Page<HcAliPay> page = new Page<HcAliPay>(pageNo, pageSize);
        IPage<HcAliPay> pageList = hcAliPayService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param hcAliPay
     * @return
     */
//    @AutoLog(value = "支付宝支付-添加")
//    @ApiOperation(value = "支付宝支付-添加", notes = "支付宝支付-添加")
    //@RequiresPermissions("org.jeecg.modules:hc_ali_pay:add")
//    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody HcAliPay hcAliPay) {
        hcAliPayService.save(hcAliPay);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param hcAliPay
     * @return
     */
//    @AutoLog(value = "支付宝支付-编辑")
//    @ApiOperation(value = "支付宝支付-编辑", notes = "支付宝支付-编辑")
    //@RequiresPermissions("org.jeecg.modules:hc_ali_pay:edit")
//    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody HcAliPay hcAliPay) {
        hcAliPayService.updateById(hcAliPay);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
//    @AutoLog(value = "支付宝支付-通过id删除")
//    @ApiOperation(value = "支付宝支付-通过id删除", notes = "支付宝支付-通过id删除")
    //@RequiresPermissions("org.jeecg.modules:hc_ali_pay:delete")
//    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        hcAliPayService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
//    @AutoLog(value = "支付宝支付-批量删除")
//    @ApiOperation(value = "支付宝支付-批量删除", notes = "支付宝支付-批量删除")
    //@RequiresPermissions("org.jeecg.modules:hc_ali_pay:deleteBatch")
//    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.hcAliPayService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "支付宝支付-通过id查询")
    @ApiOperation(value = "支付宝支付-通过id查询", notes = "支付宝支付-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<HcAliPay> queryById(@RequestParam(name = "id", required = true) String id) {
        HcAliPay hcAliPay = hcAliPayService.getById(id);
        if (hcAliPay == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(hcAliPay);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param hcAliPay
     */
    //@RequiresPermissions("org.jeecg.modules:hc_ali_pay:exportXls")
//    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HcAliPay hcAliPay) {
        return super.exportXls(request, hcAliPay, HcAliPay.class, "支付宝支付");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    //@RequiresPermissions("hc_ali_pay:importExcel")
//    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, HcAliPay.class);
    }

}
